package com.zretc.letter.listener;

import com.alibaba.fastjson.JSON;
import com.zretc.api.pojo.Letter;
import com.zretc.letter.service.LetterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
// topic需要和生产者的topic一致，consumerGroup属性是必须指定的，内容可以随意
@RocketMQMessageListener(nameServer = "127.0.0.1:9876",
        topic = "letter", consumerGroup = "letter-consumer-group",selectorExpression = "approvalMsg"
        )
public class ApprovalLetterMsgConsumerListener implements RocketMQListener<Map<String,Object>> {
    @Autowired
    private LetterService letterService;
    @Override
    public void onMessage(Map<String, Object> map) {
        String letterStr= JSON.toJSONString(map.get("letter"));
        Letter letter= JSON.parseObject(letterStr,Letter.class);
        Integer toUserId=(Integer) map.get("toUserId");
        if(letterService.publishLetterToSingleUser(letter,toUserId))
            log.info("发送成功");
        else log.error("发送失败");
    }
}
